from typing import Optional, List

from sqlalchemy import BigInteger, Boolean, Column, Date, DateTime, Index, Integer, Numeric, PrimaryKeyConstraint, \
    Sequence, SmallInteger, String, Table, Text, UniqueConstraint, text, ForeignKey
from sqlalchemy.dialects.postgresql import TIMESTAMP
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Relationship, relationship, declarative_base
import datetime

# 创建基类
Base = declarative_base()


class AmSysWebLog(Base):
    __tablename__ = 'am_sys_web_log'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='am_sys_web_log_pkey'),
        Index('ix_am_sys_web_log', 'create_time', 'ip', 'user_id'),
        {'comment': '操作日志', 'schema': 'public'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='编号')
    create_time: Mapped[datetime.datetime] = mapped_column(DateTime, comment='访问时间')
    method: Mapped[str] = mapped_column(String(255), comment='记录类名+方法名')
    user_id: Mapped[int] = mapped_column(BigInteger, comment='用户编号')
    ip: Mapped[Optional[str]] = mapped_column(String(100), comment='访问IP')
    service_name: Mapped[Optional[str]] = mapped_column(String(100), comment='模块名')
    user_name: Mapped[Optional[str]] = mapped_column(String(50), comment='用户名')
    operation_desc: Mapped[Optional[str]] = mapped_column(String(100), comment='描述')
    params: Mapped[Optional[str]] = mapped_column(String(50), comment='参数')


class AmSysApiLog(Base):
    __tablename__ = 'am_sys_api_log'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='am_sys_api_log_pkey'),
        Index('ix_am_sys_api_log', 'create_time', 'user_id'),
        {'comment': 'API访问日志', 'schema': 'public'}
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='编号')
    create_time: Mapped[datetime.datetime] = mapped_column(DateTime, comment='访问时间')
    method: Mapped[str] = mapped_column(String(255), comment='记录类名+方法名')
    user_id: Mapped[int] = mapped_column(BigInteger, comment='用户编号')
    ip: Mapped[Optional[str]] = mapped_column(String(100), comment='访问IP')
    service_name: Mapped[Optional[str]] = mapped_column(String(100), comment='模块名')
    user_name: Mapped[Optional[str]] = mapped_column(String(50), comment='用户名')
    operation_desc: Mapped[Optional[str]] = mapped_column(String(100), comment='描述')
    params: Mapped[Optional[str]] = mapped_column(String(50), comment='token')


class AmSysSqlLog(Base):
    __tablename__ = 'am_sys_sql_log'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='am_sys_sql_log_pkey'),
        Index('ix_am_sys_sql_log', 'create_time', 'datatype'),
        {'comment': '系统日志', 'schema': 'public'}
    )
    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, comment='编号')
    create_time: Mapped[datetime.datetime] = mapped_column(DateTime, comment='记录时间')
    datatype: Mapped[int] = mapped_column(BigInteger, comment='业务编号')
    dataname: Mapped[str] = mapped_column(String(60), comment='业务名称')
    dataflag: Mapped[int] = mapped_column(BigInteger, comment='日志旗标(1成功0报错)')
    msg: Mapped[str] = mapped_column(String(500), comment='日志信息')
